{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 以Lena为原始图像，通过OpenCV实现平均滤波，高斯滤波及中值滤波，比较滤波结果。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "\n",
    "img = cv.imread(r'C:\\Users\\Shen\\Desktop\\CSDN\\lena.jpg')\n",
    "img_mean = cv.blur(img,(5,5))\n",
    "img_Gaussian = cv.GaussianBlur(img,(5,5),0)\n",
    "img_median = cv.medianBlur(img,5)\n",
    "cv.imshow('img_mean',img_mean)\n",
    "cv.imshow('img_Gaussian', img_Gaussian)\n",
    "cv.imshow('img_median', img_median)\n",
    "\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()\n",
    "\n",
    "# 清晰度 ： Gaussian > Median > Mean "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 以Lena为原始图像，通过OpenCV使用Sobel及Canny算子检测，比较边缘检测结果。 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "\n",
    "img = cv.imread(r'C:\\Users\\Shen\\Desktop\\CSDN\\lena.jpg')\n",
    "imgG = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "# SOBEL\n",
    "x = cv.Sobel(imgG, cv.CV_16S,1,0)\n",
    "y = cv.Sobel(imgG, cv.CV_16S,0,1)\n",
    "adsX = cv.convertScaleAbs(x)\n",
    "adsY = cv.convertScaleAbs(y)\n",
    "sobel = cv.addWeighted(adsX, 0.5, adsY, 0.5, 0)\n",
    "cv.imshow(\"Sobel\", sobel)\n",
    "\n",
    "#Canny\n",
    "canny = cv.Canny(imgG,50,200)\n",
    "cv.imshow(\"Canny\", canny)\n",
    "\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()\n",
    "\n",
    "#########################\n",
    "#Canny 比 Sobel 的结果更清晰"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 在OpenCV安装目录下找到课程对应演示图片(安装目录\\sources\\samples\\data)，首先计算灰度直方图，进一步使用大津算法进行分割，并比较分析分割结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYcAAAD8CAYAAACcjGjIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAFkdJREFUeJzt3X+MXeWd3/H3Z+2QkGyIDQyI2k5NmtF2HaQQsMBtqqgNW2Oz1ZpKQTKqaiuyNFVE2qRq1TrdP7xNghSqdmmREiR3cbGjNA5lE2FtzHotJ9GqEhCGhAAOy3pCsjBrimfXDmGLkizZb/+4j5srnzueO2PDtT3vl3R0zvme5xw/j47lj8+Pe2+qCkmS+v3KqDsgSTr3GA6SpA7DQZLUYThIkjoMB0lSh+EgSeowHCRJHYaDJKnDcJAkdSwddQcW6vLLL6/Vq1ePuhuSdN544okn/qKqxoZpe96Gw+rVq5mcnBx1NyTpvJHkz4Zt620lSVKH4SBJ6jAcJEkdQ4VDkn+d5HCSZ5J8Ocnbklyd5LEkR5J8JclFre1b2/pU27667zifavXnktzcV9/QalNJtp/tQUqS5mfOcEiyAvhXwNqqugZYAmwG7gLurqpx4ASwre2yDThRVe8F7m7tSLKm7fc+YAPwhSRLkiwBPg9sBNYAt7e2kqQRGfa20lLg4iRLgbcDLwEfBh5s23cDt7blTW2dtv2mJGn1vVX1s6r6ITAF3NCmqap6vqp+DuxtbSVJIzJnOFTVnwP/GXiBXii8AjwB/LiqXm/NpoEVbXkF8GLb9/XW/rL++in7zFaXJI3IMLeVltP7n/zVwN8C3kHvFtCpTv7eaGbZNt/6oL5MJJlMMjk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yuqYDaufVeAeM9Zw6t4spHIb6io7zXVUdbfNjwNfoXX6+fPKSu82Pja6HZ91sY7sgz3dVvVxVv6iqvwH+O7+8vXDejzfJW+j9Y/mlqvpqK1+Q53fQWM+1c7uYwuGC/4qOJO9I8s6Ty8B64Bl649zamm0FHhpND98Qs41tH7ClvdWyDnjl5O2J89kp99X/Kb3zC73xbk7y1iRXA+PAt9/s/i1UkgD3Ac9W1e/2bbrgzu9sYz3nzu2on9y/mRO9Nxz+lN7T/t8edX/egPG9h95bDd8DDp8cI3AZcAg40uaXjrqvCxzfl+ldbv81vf9NbZttbPQuxT/fzvXTwNpR9/8sjfeLbTxP0ftH46q+9r/dxvscsHHU/Z/nWP8BvVslTwFPtumWC/H8nmas59S59RPSkqSOxXRbSZI0JMNBktRhOEiSOgwHSVKH4SBJ6jAcJEkdhoMkqcNwkCR1/D9fy9/wGHodzQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "img = cv.imread(r'C:\\Users\\Shen\\Desktop\\CSDN\\pic6.png')\n",
    "gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "# 灰度直方图\n",
    "plt.hist(gray.ravel(),256,[0,256])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import matplotlib.pyplot as plt\n",
    "img = cv.imread(r'C:\\Users\\Shen\\Desktop\\CSDN\\pic6.png')\n",
    "gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "# 大津算法\n",
    "retval, dst = cv.threshold(gray, 0, 255, cv.THRESH_OTSU )\n",
    "cv.imshow(\"dst\", dst)\n",
    "\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()\n",
    "\n",
    "########################\n",
    "#有一说一，大津算法对渐变分割很次"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 使用米粒图像，分割得到各米粒，首先计算各区域(米粒)的面积、长度等信息，进一步计算面积、长度的均值及方差，分析落在3sigma范围内米粒的数量。 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "rice_amount： 93\n"
     ]
    }
   ],
   "source": [
    "import cv2 as cv\n",
    "import copy\n",
    "\n",
    "img = cv.imread(r'C:\\Users\\Shen\\Desktop\\CSDN\\Rice.png')\n",
    "gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "_, bw = cv.threshold(gray, 0, 0xff, cv.THRESH_OTSU)\n",
    "element = cv.getStructuringElement(cv.MORPH_CROSS, (3, 3))\n",
    "bw = cv.morphologyEx(bw, cv.MORPH_OPEN, element)\n",
    "\n",
    "seg = copy.deepcopy(bw)\n",
    "bin, cnts,hier= cv.findContours(seg, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)\n",
    "#print(cnts)\n",
    "count = 0\n",
    "for i in range(len(cnts), 0, -1):\n",
    "    c = cnts[i - 1]\n",
    "    area = cv.contourArea(c)\n",
    "    if area < 10:\n",
    "        continue\n",
    "    count = count + 1\n",
    "    #print('blob', i, ':', area)\n",
    "\n",
    "    x, y, w, h = cv.boundingRect(c)\n",
    "    cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 0xff), 1)\n",
    "    cv.putText(img, str(count), (x, y), cv.FONT_HERSHEY_PLAIN, 0.5, (0, 0, 0xff))\n",
    "\n",
    "print(\"rice_amount：\", count)\n",
    "cv.imshow(\"original img\", img)\n",
    "cv.imshow(\"bw img\", bw)\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. 使用棋盘格及自选风景图像，分别使用SIFT、FAST及ORB算子检测角点，并比较分析检测结果。 \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import copy\n",
    "#img = cv.imread(r'C:\\Users\\Shen\\Desktop\\CSDN\\chessboard.png')\n",
    "#a = img.shape\n",
    "# print(a)\n",
    "#img = cv.resize(img ,(int(a[1]/5),int(a[0]/5)),interpolation=cv.INTER_CUBIC)\n",
    "img = cv.imread(r'C:\\Users\\Shen\\Desktop\\CSDN\\fengjing.jpg')\n",
    "gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "# SIFT\n",
    "img1 = copy.deepcopy(img)\n",
    "sift = cv.xfeatures2d.SIFT_create()\n",
    "keyPoint1 = sift.detect(img1)\n",
    "_, descriptor1 = sift.compute(gray, keyPoint1)\n",
    "img_sift = cv.drawKeypoints(img1, keyPoint1, img1, [0, 0, 0xff])\n",
    "# FAST\n",
    "img2 = copy.deepcopy(img)\n",
    "fast = cv.FastFeatureDetector_create()\n",
    "keyPoint2 = fast.detect(gray)\n",
    "img_fast = cv.drawKeypoints(img2, keyPoint2, img2, [0, 0, 0xff])\n",
    "# ORB\n",
    "img3 = copy.deepcopy(img)\n",
    "orb = cv.ORB_create()\n",
    "keyPoint3 = orb.detect(gray)\n",
    "img_orb = cv.drawKeypoints(img3, keyPoint3, img3, [0, 0, 0xff])\n",
    "\n",
    "\n",
    "cv.imshow('SIFT', img_sift)\n",
    "cv.imshow('FAST', img_fast)\n",
    "cv.imshow('ORB', img_orb)\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "棋盘图： SIFT 有多的中心点； ORB 比较可以 ； FAST 一个点没有\n",
    "\n",
    "风景图： SIFT 点比较全 但是多了 ； ORB 点较少 ；FAST 啥也不是，图都被点糊上去了\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(可选)使用Harris角点检测算子检测棋盘格，并与上述结果比较。 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import copy\n",
    "import numpy as np\n",
    "\n",
    "img = cv.imread(r'C:\\Users\\Shen\\Desktop\\CSDN\\chessboard.png')\n",
    "a = img.shape\n",
    "#print(a)\n",
    "img = cv.resize(img ,(int(a[1]/5),int(a[0]/5)),interpolation=cv.INTER_CUBIC)\n",
    "gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n",
    "gray = np.float32(gray)\n",
    "harris = cv.cornerHarris(gray,2,3,0.04)\n",
    "harris = cv.dilate(harris,None)\n",
    "img[harris>0.01*harris.max()]=[0,0,255]\n",
    "cv.imshow('Harris', img)\n",
    "\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Harris 效果最好 666"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ps.这次作业着实很难"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
